package com.example.booksystem.service;

import com.example.booksystem.enums.StatusEnum;
import com.example.booksystem.mapper.BookMapper;
import com.example.booksystem.model.BookInfo;
import com.example.booksystem.model.PageResult;
import com.example.booksystem.model.RequestPage;
import org.apache.ibatis.javassist.runtime.Inner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookService {

    @Autowired
    private BookMapper bookMapper;
    public Integer addBook(BookInfo bookInfo) {
        return bookMapper.insertBook(bookInfo);
    }

    public PageResult<BookInfo> getListByPage(RequestPage requestPage) {
        Integer count = bookMapper.count();

        List<BookInfo> bookInfos = bookMapper.getListByPage(requestPage.getOffset(),requestPage.getPageSize());
        //PageResult<BookInfo> result = new PageResult<>();


        for (BookInfo bookInfo:bookInfos) {
            bookInfo.setStatusCN(StatusEnum.getNameByCode(bookInfo.getStatus()).getName());
        }
        return new PageResult<>(bookInfos,count,requestPage);

    }

    public BookInfo queryBookById(Integer id){
        return bookMapper.selectBookID(id);
    }

    public void updateBook(BookInfo bookInfo) {
        bookMapper.updateBook(bookInfo);
    }

    public void deleteBook(Integer bookId) {
        bookMapper.deleteBook(bookId);
    }

    public void batchDeleteBook(List<Integer> ids) {
        bookMapper.batchDeleteBook(ids);
    }
}
